﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using ALE_Server.EC;

namespace ALE_Server
{
    public class EventCycle
    {
        private IList<TagEvent> m_Tags = new List<TagEvent>();
        private IList<string> m_LogicalReaders = new List<string>();
        private Spec m_Spec;
        private ECTerminationCondition m_TerminationCondition;
        private DateTime m_LastTagsAdded;
        private long m_StableFieldInterval;
        private long m_Duration;
        private DateTime m_StartTime;
        private DateTime m_EndTime;
        private bool m_IsRunning = true;
        private bool m_IsEventCycleStarted = false;
        private bool m_IsEventCycleTerminated = false;

        public EventCycle(Spec spec)
        {
            m_Spec = spec;
            foreach (ECLogicReader Reader in spec.ECSpec.Readers)
            {
                m_LogicalReaders.Add(Reader.Value);
            }

            if (spec.ECSpec.Boundaries.StableInterval != null)
            {
                m_StableFieldInterval = spec.ECSpec.Boundaries.StableInterval.Time;
                m_TerminationCondition = ECTerminationCondition.STABLE_SET;
            }

            if (spec.ECSpec.Boundaries.Duration != null)
            {
                m_Duration = spec.ECSpec.Boundaries.Duration.Time;
                m_TerminationCondition = ECTerminationCondition.DURATION;
            }

            m_TerminationCondition = ECTerminationCondition.UNREQUEST;
            m_StartTime = System.DateTime.Now;
        }        

        public void TagEvent(TagEvent te)
        {
            if (m_IsEventCycleStarted && !m_IsEventCycleTerminated)
            {
                if (m_LogicalReaders.Contains(te.LogicalReader))
                {
                    m_Tags.Add(te);
                    m_LastTagsAdded = System.DateTime.Now;
                }
            }
        }

        public IList<TagEvent> Tags
        {
            get
            {
                return m_Tags;
            }
        }

        public Spec Spec
        {
            get
            {
                return m_Spec;
            }
        }

        public bool IsEventCycleTerminated
        {
            get
            {
                return m_IsEventCycleTerminated;
            }
        }

        public bool IsRunning
        {
            get
            {
                return m_IsRunning;
            }
        }

        public void ShutDown()
        {
            m_EndTime = System.DateTime.Now;
            m_IsRunning = false;
        }

        public long Duration
        {
            get
            {
                return m_Duration;
            }
        }

        public DateTime StartTime
        {
            get
            {
                return m_StartTime;
            }
        }

        public DateTime EndTime
        {
            get
            {
                return m_EndTime;
            }
        }

        public DateTime LastTagAdded
        {
            get
            {
                return m_LastTagsAdded;
            }
        }

        public ECReports BuildReports()
        {
            ECReports Result = new ECReports();

            if (m_Spec.ECSpec.IncludeSpecInReports)
            {
                ECSpec Spc = new ECSpec();

                Result.Spec = Spc;
            }

            try
            {
                Result.ALEID = "";
                Result.Date = System.DateTime.Today;
                Result.TerminationCondition = ECTerminationCondition.UNREQUEST;
                Result.SpecName = m_Spec.Name;
                Result.TotalMillisSeconds = System.Convert.ToInt64(System.DateTime.Now - m_StartTime);

                foreach (ECReportSpec Spec in m_Spec.ECSpec.ReportSpecs)
                {
                    EventReport ER = new EventReport(Spec, m_Spec.GetPreviousReport(Spec.ReportName));
                    ECReport Report = ER.GetECReport();

                    Result.AddECReport(Report);
                    m_Spec.SetPreviousEventReport(Spec.ReportName, ER);
                }
            }
            catch (Exception)
            {
            }

            return Result;
        }
    }
}
